<?php
class AboutusAction extends PublicAction {
	protected $_title = 'About Us';
	protected $_cate_title =  'About Us';
	protected $_cate_table = 'cms_cate';
	protected $_cate_desc_table = 'cms_cate_description';
	protected $_inner_table = 'cms_inner';
	protected $_inner_desc_table = 'cms_inner_description';
	protected $_cate_to_inner = 'cms_cate_to_inner';
	protected $_page = 10 ;
	protected $_parent_id = 4;
	
	
	public function index($value='')
	{
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "sort_order";
			$_GET['updown'] = $upDown = "desc";
				
		}
		import("ORG.Util.Page");
		$page = $this->_page;
		$Model = M($this->_cate_table)->where(array('parent_id' => $this->_parent_id));
	
		$count = $Model->count();
	
		$Page = new Page($count, $page);
		$list   = $this->_get_cate_list($Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
	
	
		$page = $Page->show();
	
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		foreach($list as $v){
			$v['get_entries'] = $this->_getEntries($v['cate_id']);
			$rows[] = $v;
		}
		$this->page = $page;
		$this->listing = $rows;
		$this->title = $this->_cate_title;
		$this->display();
	}
	public function detail(){
		$editId = I('get.id','',intval);
		$lang = array();
		$langList = explode(",", C('LANG_LIST'));
		foreach($langList as $item)
		{
			$langTxt = '';
			if ($item == 'tchinese') $langTxt = '繁體';
			if ($item == 'schinese') $langTxt = '简体';
			if ($item == 'english')  $langTxt = 'English';
			$lang[] = array("value" => $item, 'txt'=> $langTxt );
		}
		$this->editId = $editId;
		$this->langList = $langList;
		$this->lang = $lang;
		if(tep_not_null($editId) && is_numeric($editId)){
			/*編輯*/
			$cateInfo   = $this->_getCateData($editId);
			$cateToLang  = $this->_getCateDateLang($editId);
			foreach ($cateToLang as $value)
			{
				$cate_name[$value['language_id']]   =   $value['cate_name'];
			}
				
			if ($cateInfo["cate_image"] != '')
			{
				$imageShow = '<input type="hidden"  name="cate_image_del" value="f" />
								  <div id="image">
								  <a  href="'.__ROOT__.'/Public/images/cms/'.$cateInfo["cate_image"].'" class="fancyBox">
								  <img src="'.__ROOT__.'/Public/images/cms/'.$cateInfo["cate_image"].'" width="200"></a>
								  <br /><a  href="#" onclick="delImages();return false;"  class="del">Delete</a></div>';
			}
			$this->cate_name = $cate_name;
			$this->cateInfo = $cateInfo;
			$this->imageShow = $imageShow;
			$this->editId = $editId;
		}
		$this->title = $this->_cate_title;
		$this->parent_id = $this->_parent_id;
		$this->display();
	}
	public function inner_index(){
		$cid = I('get.cid','',intval);
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "date";
			$_GET['updown'] = $upDown = "desc";
				
		}
		import("ORG.Util.Page");
		$page = $this->_page;
		$count = sizeof($this->_get_inner_list($cid));
		$Page = new Page($count, $page);
		$list   = $this->_get_inner_list($cid,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		$page = $Page->show();
	
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		$this->category = $this->_getCateData($cid,'cate_name');
		$this->cid = $cid;
		$this->page = $page;
		$this->listing = $list;
		$this->title = $this->_title;
		$this->display();
	}
	public function  inner_detail(){
		$cid = I('get.cid');
		$editId = I('pid','',intval);
		$lang = array();
		$langList = explode(",", C('LANG_LIST'));
		foreach($langList as $item)
		{
			$langTxt = '';
			if ($item == 'tchinese') $langTxt = '繁體';
			if ($item == 'schinese') $langTxt = '简体';
			if ($item == 'english')  $langTxt = 'English';
			$lang[] = array("value" => $item, 'txt'=> $langTxt );
		}
		$this->editId = $editId;
		$this->langList = $langList;
		$this->lang = $lang;
	
		if(is_numeric($editId)){
				
			/*編輯*/
			$innerInfo   = $this->_getInnerData($editId);
			$innerToLang  = $this->_getInnerDataLang($editId);
			foreach ($innerToLang as $value)
			{
				$inner_title[$value['language_id']]             =   $value['inner_title'];
				$inner_short_description[$value['language_id']] =   $value['inner_short_description'];
				$inner_description[$value['language_id']]       =   $value['inner_description'];
				$speaker[$value['language_id']]                 =   $value['speaker'];
			}
				
			if ($innerInfo["image"] != '')
			{
				$imageShow = '<input type="hidden"  name="inner_image_del" value="f" />
								  <div id="image">
								  <a  href="'.__ROOT__.'/Public/images/cms/'.$innerInfo["image"].'" class="fancyBox">
								  <img src="'.__ROOT__.'/Public/images/cms/'.$innerInfo["image"].'" width="200"></a>
								  <br /><a  href="#" onclick="delImages();return false;"  class="del">Delete</a></div>';
			}
			if ($innerInfo["highLight_image"] != '')
			{
				$highlight_imageShow = '<input type="hidden"  name="inner_highlight_image_del" value="f" />
								  <div id="image2">
								  <a  href="'.__ROOT__.'/Public/images/cms/'.$innerInfo["highLight_image"].'" class="fancyBox">
								  <img src="'.__ROOT__.'/Public/images/cms/'.$innerInfo["highLight_image"].'" width="200"></a>
								  <br /><a  href="#" onclick="delImages2();return false;"  class="del">Delete</a></div>';
			}
			$this->inner_title             = $inner_title;
			$this->inner_short_description = $inner_short_description;
			$this->inner_description       = $inner_description;
			$this->speaker                 = $speaker;
			$this->innerInfo               = $innerInfo;
			$this->imageShow               = $imageShow;
			$this->highlight_imageShow     = $highlight_imageShow;
			$this->editId                  = $editId;
		}
		$this->cid = $cid;
		$this->title = $this->_title;
		$this->display();
	}
	protected function _get_cate_list($limit = '',$order = '')
	{
		$Qcate = M()->limit($limit)->order($order)
		->query('select c.*,cd.* from '.$this->_cate_table.' c, '.$this->_cate_desc_table.' cd
							  where c.parent_id = '.$this->_parent_id.' and c.cate_id = cd.cate_id and  cd.language_id= '.L('lan_id').'
							  %ORDER%  %LIMIT% ',true);
	
		return $Qcate;
	}
	protected function _get_inner_list($cid = null,$limit = '',$order = '')
	{
		$Qinner = M()->limit($limit)->order($order)
		->query('select i.*,id.* from '.$this->_inner_table.' i, '.$this->_inner_desc_table.' id, '.$this->_cate_to_inner.' c2i
						 where i.inner_id = id.inner_id and i.inner_id=c2i.inner_id and c2i.cate_id= '.$cid.' and id.language_id='.L('lan_id').'
						 %ORDER%  %LIMIT% ',true);
		return $Qinner;
	}
	
	
	protected function _getCateData($id,$key= null)
	{
		$Qcate = M()->query('select c.*,cd.* from '.$this->_cate_table.' c, '.$this->_cate_desc_table.' cd
							 where c.cate_id = cd.cate_id and c.cate_id='.$id.'  and  cd.language_id='.L('lan_id').'');
		if(is_null($key) && empty($key)){
			return $Qcate[0];
		}else{
			return $Qcate[0][$key];
		}
	
	}
	protected  function _getCateDateLang($id){
		$Qcate =  M($this->_cate_desc_table)->where(array('cate_id' => $id))->select();
		return $Qcate;
	}
	
	protected function _getInnerData($id, $key = null)
	{
		$Qinner = M()->query('select i.*,id.* from '.$this->_inner_table.' i, '.$this->_inner_desc_table.' id
							where i.inner_id = id.inner_id and i.inner_id='.$id.' and  id.language_id='.L('lan_id').'');
		if(empty($key) && is_null($key)){
			return $Qinner[0];
		}else{
			return $Qinner[0][$key];
		}
	
	}
	protected  function _getInnerDataLang($id){
		$QInner =  M($this->_inner_desc_table)->where(array('inner_id' => $id))->select();
		return $QInner;
	}
	protected function _getEntries($cId)
	{
		$count = M($this->_cate_to_inner)->where(array('cate_id'=>$cId))->count();
		return $count;
	}
	public function save()
	{
		$id = I('post.id',null);
		$data = array(
				'status'     => I('post.status'),
				'parent_id'     => I('post.parent_id'),
				'sort_order' => I('post.sort_order'),
				'cate_name'  => I('post.cate_name'),
		);
		$new_id = null;
	
		if ( is_numeric($id) )
		{
			$data['last_modified']  =  date('Y-m-d H:i:s');
			M($this->_cate_table)->where(array('cate_id' => $id))->save($data);
		}
		else
		{
			$data['date_added']  = date('Y-m-d H:i:s');
			$new_id = M($this->_cate_table)->add($data);
		}
		$cate_id = (is_numeric($id)) ? $id : $new_id;
	
		$language = explode(",", C('LANG_LIST'));
		$i = 0;
		foreach( $language as $item)
		{
			$i++;
			$map = array();
			$where = array();
			$map['cate_name'] = $data['cate_name'][$i];
			if ( is_numeric($id) ) {
				$where['cate_id'] = $cate_id;
				$where['language_id'] = $i;
				M($this->_cate_desc_table)->where($where)->save($map);
			} else {
				$map['cate_id'] = $cate_id;
				$map['language_id'] = $i;
				M($this->_cate_desc_table)->add($map);
			}
		}
		$cate_image = M($this->_cate_table)->where(array('cate_id' => $cate_id))->getField('cate_image');
	
		// image
		if ($_FILES['cate_image']['error'] == 0)
		{
			//上传图片
			import('@.ORG.UploadFile');
			$upload = new UploadFile('cate_image');
			//设置上传文件大小
			$upload->maxSize            = 6000000;
			//设置上传文件类型
			$upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
			//设置附件上传目录
			$upload->savePath 			=  './Public/images/cms/';
			//设置上传文件规则
			$upload->saveRule           = 'uniqid';
				
			if($upload->upload())
			{
				@unlink("./Public/images/cms/".$cate_image);
				$info =  $upload->getUploadFileInfo();
				M($this->_cate_table)->where(array("cate_id" => $cate_id))->setField('cate_image',$info[0]['savename']);
			}else{
				S('err','image save failed! '.$upload->getErrorMsg());
				$this->redirect('index');
			}
		}
		elseif (isset($_POST['cate_image_del']) && $_POST['cate_image_del'] == 't')
		{
			@unlink("./Public/images/cms/".$cate_image);
			M($this->_cate_table)->where(array("cate_id" => $cate_id))->setField('cate_image','');
		}
		S('success',$this->_cate_title.' successfully added!');
		$this->redirect('index');
			
	}
	public function save_inner()
	{
		$cid = I('get.cid');
		$id = I('post.id',null);
		$data = array(
				'inner_title' => I('post.inner_title'),
				'inner_short_description' => I('post.inner_short_description'),
				'inner_description' => I('post.inner_description'),
				'speaker' => I('post.speaker'),
				'highLight' => I('post.highLight'),
				'status' =>  I('post.status'),
				'sort_order' =>  I('post.sort_order'),
				'date' => I('post.date'),
		);
		$new_id = null;
		if ( is_numeric($id) )
		{
			$data['last_modified']  =  date('Y-m-d H:i:s');
			M($this->_inner_table)->where(array('inner_id' => $id))->save($data);
		}
		else
		{
			$data['date_added']  = date('Y-m-d H:i:s');
			$new_id = M($this->_inner_table)->add($data);
			$cate_to_inner['inner_id'] = $new_id;
			$cate_to_inner['cate_id']  = $cid;
			M($this->_cate_to_inner)->add($cate_to_inner);
		}
		//echo M()->getLastSql();
		//exit;
		$inner_id = (is_numeric($id)) ? $id : $new_id;
	
		$language = explode(",", C('LANG_LIST'));
		$i = 0;
		foreach( $language as $item)
		{
			$i++;
			$map = array();
			$where = array();
				
			$map['speaker']                 = $data['speaker'][$i];
			$map['inner_title']             = $data['inner_title'][$i];
			$map['inner_short_description'] = $data['inner_short_description'][$i];
			$map['inner_description']       = $data['inner_description'][$i];
			if ( is_numeric($id) ) {
				$where['inner_id'] = $inner_id;
				$where['language_id'] = $i;
				M($this->_inner_desc_table)->where($where)->save($map);
			} else {
				$map['inner_id'] = $inner_id;
				$map['language_id'] = $i;
				M($this->_inner_desc_table)->add($map);
			}
			//echo M()->getLastSql();
	
		}
		//exit;
		$image = M($this->_inner_table)->where(array('inner_id' => $inner_id))->getField('image');
	
		// image
		if ($_FILES['image']['error'] == 0)
		{
			import('@.ORG.UploadFile');
			$upload = new UploadFile('image');
			$upload->maxSize            = 6000000;
			$upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
			$upload->savePath 			=  './Public/images/cms/';
			$upload->saveRule           = 'uniqid';
			$info =  $upload->uploadOne($_FILES['image']);
			if($info)
			{
				@unlink("./Public/images/cms/".$image);
				M($this->_inner_table)->where(array("inner_id" => $inner_id))->setField('image',$info[0]['savename']);
			}else{
				S('err','image save failed! '.$upload->getErrorMsg());
				$this->redirect('inner_index',array('cid' => $cid));
			}
		}
		elseif (isset($_POST['inner_image_del']) && $_POST['inner_image_del'] == 't')
		{
			@unlink("./Public/images/cms/".$image);
			M($this->_inner_table)->where(array("inner_id" => $inner_id))->setField('image','');
		}
		$inner_highLight_image = M($this->_inner_table)->where(array('inner_id' => $inner_id))->getField('highLight_image');
		// highLight_image
		if ($_FILES['highLight_image']['error'] == 0)
		{
	
			import('@.ORG.UploadFile');
			$upload = new UploadFile('highLight_image');
			$upload->maxSize            = 6000000;
			$upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
			$upload->savePath 			=  './Public/images/cms/';
			$upload->saveRule           = 'uniqid';
			$info =  $upload->uploadOne($_FILES['highLight_image']);
			if($info)
			{
				@unlink("./Public/images/cms/".$inner_highLight_image);
				M($this->_inner_table)->where(array("inner_id" => $inner_id))->setField('highLight_image',$info[0]['savename']);
			}else{
				S('err','HighLight image save failed! '.$upload->getErrorMsg());
				$this->redirect('inner_index',array('cid' => $cid));
			}
		}
		elseif (isset($_POST['inner_highlight_image_del']) && $_POST['inner_highlight_image_del'] == 't')
		{
			@unlink("./Public/images/cms/".$inner_highLight_image);
			M($this->_inner_table)->where(array("inner_id" => $inner_id))->setField('highLight_image','');
		}
		S('success',$this->_title.' successfully added!');
		$this->redirect('inner_index',array('cid' => $cid));
	
	}
	public function deleteCate()
	{
		$id = I('id');
		if(is_numeric($id)){
			$image = M($this->_cate_table)->where(array("cate_id" => $id))->getField('cate_image');
			@unlink("./Public/images/cms/".$image);
			M($this->_cate_table)->where(array('cate_id' => $id))->delete();
			M($this->_cate_desc_table)->where(array('cate_id' => $id))->delete();
			$inner = M($this->_cate_to_inner)->where(array('cate_id' => $id))->select();
			foreach($inner as $v){
				M($this->_inner_table)->where(array('inner_id' => $v['inner_id']))->delete();
				M($this->_inner_desc_table)->where(array('inner_id' => $v['inner_id']))->delete();
			}
			M($this->_cate_to_inner)->where(array('cate_id' => $id))->delete();
			S('success',$this->_cate_title.' deleted successfully!');
			$this->redirect('index');
		}else{
			S('err',$this->_cate_title.' deleted failure!');
			$this->redirect('index');
		}
	
	}
	public function deleteInner()
	{
		$id  = I('id');
		$cid = I('cid');
		if(is_numeric($id)){
			$inner_info = $this->_getInnerData($id);
			@unlink("./Public/images/cms/".$inner_info['image']);
			@unlink("./Public/images/cms/".$inner_info['highLight_image']);
			M($this->_inner_table)->where(array('inner_id' => $id))->delete();
			M($this->_inner_desc_table)->where(array('inner_id' => $id))->delete();
			M($this->_cate_to_inner)->where(array('inner_id' => $id))->delete();
			S('success',$this->_title.' deleted successfully!');
			$this->redirect('inner_index',array('cid' => $cid));
		}else{
			S('err',$this->_title.' deleted failure!');
			$this->redirect('inner_index',array('cid' => $cid));
		}
	
	}
	public  function changeStatus(){
		$check = M($this->_cate_table)->where(array("cate_id" => I('get.id')))->getField('status');
		$cate_status = ($check == 1)?0:1;
		M($this->_cate_table)->where(array("cate_id" =>I('get.id')))->setField('status',$cate_status);
		echo (int)$cate_status;
	}
	public function changeInnerStatus(){
		$check = M($this->_inner_table)->where(array("inner_id" => I('get.id')))->getField('status');
		$inner_status = ($check == 1)?0:1;
		M($this->_inner_table)->where(array("inner_id" =>I('get.id')))->setField('status',$inner_status);
		echo (int)$inner_status;
	
	}
	
}